package com.smartjobs.action;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;
import com.smartjobs.bean.JobPostingVO;
import com.smartjobs.loader.MergeFile;
import com.smartjobs.loader.SwoopTest;
import com.smartjobs.reasoner.ReasonerQuery;
import com.smartjobs.util.RuntimeSettings;

@SuppressWarnings("serial")
public class JobPostingAction extends ActionSupport {

	private ArrayList<JobPostingVO> jobpostings = new ArrayList<JobPostingVO>();
	String company = "";
	String location = "";
	String skills = "";
	String refresh = "";

	public String getRefresh() {
		return refresh;
	}

	public void setRefresh(String refresh) {
		this.refresh = refresh;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getSkills() {
		return skills;
	}

	public void setSkills(String skills) {
		this.skills = skills;
	}

	public ArrayList<JobPostingVO> getJobpostings() {
		return jobpostings;
	}

	public void setJobpostings(ArrayList<JobPostingVO> jobpostings) {
		this.jobpostings = jobpostings;
	}

	public String execute() {
		int i;
		ArrayList<JobPostingVO> jobPostingList = new ArrayList<JobPostingVO>();

		if (!refresh.equals("")) {
			System.out.println("inside refresh");
			try {
				saveUrl(RuntimeSettings.location + "/"
						+ RuntimeSettings.DDSFilename, RuntimeSettings.DDSUrl
						+ "/" + RuntimeSettings.DDSFilename);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			refresh = "";

			new SwoopTest().addSemantics(RuntimeSettings.location
					+ "/smartJobs.rdf");
			MergeFile.mergeFile();
		}

		// RDFLoader.loadFromDataBase();
		jobPostingList = new ReasonerQuery().getAllJobs();

		for (i = 0; i < jobPostingList.size(); i++) {

			if ((jobPostingList.get(i).getCompanyName().toLowerCase()
					.contains(company.toLowerCase()))
					&& (jobPostingList.get(i).getLocation().toLowerCase()
							.contains(location.toLowerCase()))
					&& (jobPostingList.get(i).getSkills().toLowerCase()
							.contains(skills.toLowerCase()))) {

				if (jobPostingList.get(i).getSource()
						.equalsIgnoreCase("www.smartJobs.com")) {
					jobPostingList.get(i).setSourceFlag(1);
					System.out.println("source: "
							+ jobPostingList.get(i).getSource()
							+ " setting source flag to 1");

				} else {
					jobPostingList.get(i).setSourceFlag(0);
				}

				System.out.println("source flag: "
						+ jobPostingList.get(i).getSourceFlag());
				jobpostings.add(jobPostingList.get(i));
			}
		}
		return SUCCESS;
	}

	public String init() {
		return SUCCESS;

	}

	public void saveUrl(String filename, String urlString)
			throws MalformedURLException, IOException {
		BufferedInputStream in = null;
		FileOutputStream fout = null;
		URLConnection uc;
		try {

			URL urlc = new URL(urlString);
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					"192.16.3.254", 8080));
			uc = (URLConnection) urlc.openConnection(proxy);
			uc.addRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2");
			uc.connect();

			in = new BufferedInputStream(uc.getInputStream());
			fout = new FileOutputStream(filename);

			byte data[] = new byte[1024];
			int count;
			while ((count = in.read(data, 0, 1024)) != -1) {
				fout.write(data, 0, count);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (in != null)
				in.close();
			if (fout != null)
				fout.close();
		}
	}

}
